package com.hroze.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hroze.util.JsonUtil;
import com.hroze.util.web.Prop;
import com.hroze.util.web.SessionManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * ajax 使用struts2通用超类
 * 
 * @author ahoo
 * 
 */
public class SuperAction extends ActionSupport {

	protected Map<String, Object> map;
	protected String sessionId;
	protected static Prop config;
	protected static String path;
	protected final Logger log = Logger.getLogger(this.getClass());

	protected Map<String, Object> getMap() {
		return map;
	}

	protected void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 使用特定的 map 返回结果集
	 * 
	 * @param map
	 * @throws IOException
	 */
	public void json(Map<String, Object> map) throws IOException {
		HttpServletResponse response = this.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		String json = JsonUtil.objToJson(map);
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	/**
	 * 只返回数据结果集
	 * 
	 * @param res
	 * @return
	 * @throws IOException
	 */
	protected String data() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		String json = JsonUtil.objectToJson(this.map.get("data"));
		this.write(json);
		return null;
	}

	public static void load() {
		if (config == null) {
			config = new Prop(SuperAction.class);
		} else {
			config.reload();
		}
	}

	/**
	 * 使用父类的map 变量序列化
	 * 
	 * @param fields
	 * @return
	 * @throws IOException
	 */
	protected String json(String... fields) throws IOException {
		return json(this.map, fields);
	}

	protected void setCheckCodeError() {
		this.map.put("msg", config.get("CHECK_CODE_ERROR"));
	}

	protected void setUnLoginError() {
		this.map.put("msg", config.get("UN_LOGIN_ERROR"));
	}

	protected void setNoSuchUserError() {
		this.map.put("msg", config.get("NO_SUCH_USER_ERROR"));
	}

	/**
	 * 返回json结果集，指定被序列化的字段
	 * 
	 * @param res
	 * @return
	 * @throws IOException
	 */
	protected String json(Map<String, Object> res, String... fields)
			throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		String json = JsonUtil.objectToJson(res, fields);
		this.write(json);
		return null;
	}

	protected HttpSession getSession() {
		return this.getRequest().getSession();
	}

	/**
	 * 获得sission属性值
	 * 
	 * @param name
	 *            属性名称
	 * @return
	 */
	protected Object getAttribute(String name) {
		if (this.getSession() == null) {
			return null;
		} else {
			return this.getSession().getAttribute(name);
		}
	}

	protected HttpSession getSessionById(String id) {
		return SessionManager.getSessionById(id);
	}

	/**
	 * 根据session编号获得会话
	 * 
	 * @return
	 */
	protected HttpSession getSessionById() {
		if (StringUtils.isBlank(this.sessionId)) {
			return null;
		}
		return getSessionById(this.sessionId);
	}

	protected PrintWriter getWriter() throws IOException {
		return this.getResponse().getWriter();
	}

	protected void write(String text) throws IOException {
		this.getWriter().println(text);
	}

	protected Object getUser() {
		return this.getAttribute(config.get("user"));
	}

	/**
	 * check the user isLogin
	 * 
	 * @return
	 */
	protected boolean isLogIn() {
		if (this.getUser() != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查验证码是否正确
	 * 
	 * @return
	 */
	protected boolean validCode(String checkCode) {
		String user_msg = (String) getAttribute(config.get("CHECK_CODE"));
		if (user_msg != null && user_msg.equals(checkCode))
			return true;
		else
			return false;
	}

	protected void initMap() {
		this.map = new HashMap<String, Object>();
		this.map.put("success", false);
		this.map.put("data", "[]");
		this.map.put("time", new Date());
		this.map.put("size", 0);
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	/**
	 * 相应客户端
	 * 
	 * @throws IOException
	 */
	public String json() throws IOException {
		json(this.map);
		return null;
	}

	public void setTime(Date time) {
		this.map.put("time", time);
	}

	public void setTime(Long time) {
		this.map.put("time", time);
	}

	public void setTrue() {
		this.map.put("success", true);
	}

	public void setFalse() {
		this.map.put("success", false);
	}

	public void setMsg(String msg) {
		this.map.put("msg", msg);
	}

	public void setScript(String script) {
		this.map.put("script", script);
	}

	public void setData(Object data) {
		this.map.put("data", data);
	}

	public void setSize(Object size) {
		this.map.put("size", size);
	}
}
